package com.cjm.one.order.service.impl;

import com.cjm.one.common.utils.TimeRangeUtils;
import com.cjm.one.model.custom.dto.UserStatsDTO;
import com.cjm.one.model.order.dto.OrderChartItem;
import com.cjm.one.model.order.dto.OrderStatsDTO;
import com.cjm.one.model.order.dto.OrderSummary;
import com.cjm.one.model.order.dto.TimeRange;
import com.cjm.one.order.mapper.OrderInfoMapper;
import com.cjm.one.order.service.StatsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

@Service
public class StatsServiceImpl implements StatsService {
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Override
    public Map<String, Object> getOrderSummary(String timeRange) {
        // 解析时间范围
        TimeRange currentRange = TimeRangeUtils.parseTimeRange(timeRange);
        TimeRange compareRange = TimeRangeUtils.getCompareRange(timeRange);

        // 查询数据
        OrderStatsDTO current = buildStats(currentRange);
        OrderStatsDTO compare = buildStats(compareRange);
        current.calculateGrowthRates(compare);

        // 组装响应
        return Map.of(
                "indicators", List.of(
                        buildIndicator("订单金额", current.getOrderAmount(), current.getAmountGrowthRate(), "元"),
                        buildIndicator("订单数量", current.getOrderCount(), current.getCountGrowthRate(), "单"),
                        buildIndicator("退款金额", current.getRefundAmount(), current.getRefundGrowthRate(), "元"),
                        buildIndicator("退款订单", current.getRefundOrderCount(), current.getRefundOrderGrowthRate(), "%")
                )
        );
    }

    @Override
    public List<OrderChartItem> getOrderChartData(String timeRange) {
        TimeRange range = TimeRangeUtils.parseTimeRange(timeRange);
        return orderInfoMapper.selectOrderChartData(
                range.getStartTime(),
                range.getEndTime()
        );
    }

    private Map<String, Object> buildIndicator(String title, Object value,
                                               BigDecimal rate, String unit) {
        return Map.of(
                "title", title,
                "value", value,
                "rate", rate.setScale(1, RoundingMode.HALF_UP),
                "unit", unit,
                "color", rate.compareTo(BigDecimal.ZERO) >= 0 ? "#67C23A" : "#F56C6C"
        );
    }


    private OrderStatsDTO buildStats(TimeRange range) {
        return new OrderStatsDTO()
                .setOrderAmount(countOrdersAmout(range))
                .setRefundOrderCount(countRefundOrders(range))
                .setOrderCount(countOrders(range))
                .setRefundAmount(countRefundAmount(range));
    }

    private BigDecimal countOrdersAmout(TimeRange range) {
        return orderInfoMapper.selectOrderAmount(range);
    }
    private Long countOrders(TimeRange range) {
        return orderInfoMapper.selectOrderCount(range);
    }

    private BigDecimal countRefundAmount(TimeRange range) {
        return orderInfoMapper.selectRefundAmount(range);
    }
    /**
     * @description: 退款订单
     **/
    private Long countRefundOrders(TimeRange range) {
        return orderInfoMapper.selectRefundOrderCount(range);
    }






}
